package com.fastloan.mgt.daily.controller;

import com.fastloan.mgt.daily.dao.WorkContentMapper;
import com.fastloan.mgt.daily.dao.WorkLocationMapper;
import com.fastloan.mgt.daily.model.WCustomerManagerDaily;
import com.fastloan.mgt.daily.model.WorkContent;
import com.fastloan.mgt.daily.model.WorkLocation;
import com.fastloan.mgt.daily.model.vo.WCustomerManagerDailyVo;
import com.fastloan.mgt.daily.service.WCustomerManagerDailyService;
import com.fastloan.mgt.daily.service.WorkInfoService;
import com.fastloan.safe.core.util.ManagerKit;
import com.fastloan.safe.mgt.org.model.CmuOrg;
import com.fastloan.safe.mgt.org.service.CmuOrgService;
import com.fastloan.utils.excelutil.exception.ExpertExcelException;
import com.kawins.base.baseabs.BaseController;
import com.kawins.result.ResultEnum;
import com.kawins.result.ResultTo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 微信客户经理日报Controller
 *
 * @author yanwe
 *         createTime 2017-09-2017/9/15 14:13
 */
@RestController
@RequestMapping("/api/customerManagerDay/wx")
public class CustomerManagerWxDailyController extends BaseController<WCustomerManagerDaily> {

    @Autowired
    private WCustomerManagerDailyService customerManagerDailyService;

    @Autowired
    private WorkInfoService workInfoService;

    @Autowired
    private CmuOrgService organizationService;

    @GetMapping("/statistics")
    public ResultTo selectDailyStatistics(){
        return customerManagerDailyService.selectDailyStatistics();
    }

    @RequestMapping("/pageList")
    public ResultTo selectCustomerManagerDailyList(HttpServletRequest request){
        Map<String,Object> map = new HashMap<>();
        String orgId = Optional.ofNullable(request.getParameter("orgId"))
                .map(Object::toString).orElse(null);
        List<String> orgIds = organizationService.
                selectTopOrgBySon(orgId,true,false).stream().map(CmuOrg::getId).collect(Collectors.toList());
        map.put("orgIds",orgIds);
        return dataPage("selectCustomerManagerDailyList",map);
    }

    @GetMapping()
    public ResultTo getWXDaily(@RequestParam(value = "dailyCommitId",required = false) Integer dailyCommitId){
        WCustomerManagerDailyVo daily;
        if(dailyCommitId == null){
            daily = customerManagerDailyService.getDaily(ManagerKit.getUserId(), LocalDateTime.now());
        } else {
            daily = customerManagerDailyService.getDaily(dailyCommitId);
        }
        return new ResultTo().setData(daily);
    }

    @GetMapping("/excel")
    public ResultTo outPutExcel(HttpServletRequest request, HttpServletResponse response){
        try {
            long start = System.currentTimeMillis();
            customerManagerDailyService.outPutExcel(request,response);
            log.info("微信日报导出耗时:",System.currentTimeMillis() - start+"ms");
        } catch (ExpertExcelException e) {
            e.printStackTrace();
            log.error("日报导出错误","日报导出错误");
            return new ResultTo(ResultEnum.OPERATION_FAILED);
        }
        return new ResultTo();
    }

    @GetMapping("/workInfo")
    public ResultTo getWorkInfo(){
        return workInfoService.getWorkInfo(ManagerKit.getTopOrgId());
    }

    @GetMapping("/workContent")
    public ResultTo getWorkContent(@RequestParam(value = "workContentId",required = false) Integer workContentId){
        return new ResultTo().setData(workInfoService.getWorkContent(ManagerKit.getTopOrgId()
                ,workContentId == null ? 0 : workContentId,null));
    }

    @GetMapping("/workLocation")
    public ResultTo getWorkLocation(@RequestParam(value = "workLocationId",required = false) Integer workLocationId){
        return new ResultTo().setData(workInfoService.getWorkLocation(ManagerKit.getTopOrgId()
                ,workLocationId == null ? 0 : workLocationId , ManagerKit.getUserId()));
    }

    @PostMapping("/workContent")
    public ResultTo insertWorkContent(WorkContent workContent){
        workContent.setOrgId(ManagerKit.getTopOrgId());
        workContent.setCreateTime(new Date());
        workContent.setModifyTime(new Date());
        workContent.setUserId(null);
        if (workContent.getWorkContentParentId() == null){
            workContent.setWorkContentParentId(0);
        }
        workInfoService.getWorkContentMapper().insert(workContent);
        return new ResultTo();
    }

    @PostMapping("/workLocation")
    public ResultTo insertWorkLocation(WorkLocation workLocation){
        workLocation.setOrgId(ManagerKit.getTopOrgId());
        workLocation.setCreateTime(new Date());
        workLocation.setModifyTime(new Date());
        workLocation.setUserId(null);
        if (workLocation.getWorkLocationParentId() == null || workLocation.getWorkLocationParentId() == 0){
            workLocation.setWorkLocationParentId(0);
        }
        workInfoService.getWorkLocationMapper().insert(workLocation);
        return new ResultTo();
    }

    @PutMapping("/workContent")
    public ResultTo updateWorkContent(WorkContent workContent){
        workInfoService.getWorkContentMapper().updateByPrimaryKeySelective(workContent);
        return new ResultTo();
    }

    @PutMapping("/workLocation")
    public ResultTo updateWorkLocation(WorkLocation workLocation){
        workInfoService.getWorkLocationMapper().updateByPrimaryKeySelective(workLocation);
        return new ResultTo();
    }

    @DeleteMapping("/workContent/{workContentId}")
    public ResultTo deleteWorkContent(@PathVariable("workContentId") Integer workContentId){
        WorkContentMapper workContentMapper = workInfoService.getWorkContentMapper();
        int count = workContentMapper.selectCount(new WorkContent().setWorkContentParentId(workContentId));
        if (count != 0){
            return new ResultTo(ResultEnum.OPERATION_FAILED,"该内容有子内容,请先删除子内容！");
        }
        workContentMapper.deleteByPrimaryKey(workContentId);
        return new ResultTo();
    }

    @DeleteMapping("/workLocation/{workLocationId}")
    public ResultTo deleteWorkLocation(@PathVariable("workLocationId") Integer workLocationId){
        WorkLocationMapper workLocationMapper = workInfoService.getWorkLocationMapper();
        int count = workLocationMapper.selectCount(new WorkLocation().setWorkLocationParentId(workLocationId));
        if (count != 0){
            return new ResultTo(ResultEnum.OPERATION_FAILED,"该地点有子地点,请先删除子地点！");
        }
        workLocationMapper.deleteByPrimaryKey(workLocationId);
        return new ResultTo();
    }

}
